PuLP is an open source linear programming package for python. PuLP can be installed using pip, instructions here.
In this notebook, we'll explore how to construct and solve the linear programming problem described in Part 1 using PuLP.
A brief reminder of our linear programming problem:
We want to find the maximum solution to the objective function:
Z = 4x + 3y
Subject to the following constraints:
x ≥ 0
y ≥ 2
2y ≤ 25 - x
4y ≥ 2x - 8
y ≤ 2x - 5
We'll begin by importing PuLP
In [1]:
import pulp
Then instantiate a problem class, we'll name it "My LP problem" and we're looking for an optimal maximum so we use LpMaximize
In [2]:
my_lp_problem = pulp.LpProblem("My LP Problem", pulp.LpMaximize)
We then model our decision variables using the LpVariable class. In our example, x had a lower bound of 0 and y had a lower bound of 2.
Upper bounds can be assigned using the upBound parameter.
In [3]:
x = pulp.LpVariable('x', lowBound=0, cat='Continuous')
y = pulp.LpVariable('y', lowBound=2, cat='Continuous')
The objective function and constraints are added using the += operator to our model.
The objective function is added first, then the individual constraints.
In [4]:
# Objective function
my_lp_problem += 4 * x + 3 * y, "Z"
# Constraints
my_lp_problem += 2 * y <= 25 - x
my_lp_problem += 4 * y >= 2 * x - 8
my_lp_problem += y <= 2 * x - 5
We have now constructed our problem and can have a look at it.
In [5]:
my_lp_problem
Out[5]:
PuLP supports open source linear programming solvers such as CBC and GLPK, as well as commercial solvers such as Gurobi and IBM's CPLEX.
The default solver is CBC, which comes packaged with PuLP upon installation.
For most applications, the open source CBC from COIN-OR will be enough for most simple linear programming optimisation algorithms.
In [6]:
my_lp_problem.solve()
pulp.LpStatus[my_lp_problem.status]
Out[6]:
We have also checked the status of the solver, there are 5 status codes:
We can now view our maximal variable values and the maximum value of Z.
We can use the varValue method to retrieve the values of our variables x and y, and the pulp.value function to view the maximum value of the objective function.
In [7]:
for variable in my_lp_problem.variables():
print "{} = {}".format(variable.name, variable.varValue)
In [8]:
print pulp.value(my_lp_problem.objective)
Same values as our manual calculations in part 1.
In the next part we'll be looking at a more real world problem.